from django.db.models import Q, Count
from django.shortcuts import render
from django.http import HttpResponse
from .models import *

def index(request):
    all = Question.objects.all()
    return render(request, 'index.html', {"alls": all})
def suanfen(request):
    if request.method == 'POST':
        xuanze = request.POST.getlist('nnn')  #拿到对应的答案和提号 [ture,1]
        a = 0
        for i in xuanze:
            s =  i.split(",")  #循环拿到的多个xuanze,每个用逗号隔开  ture 1
            q = s[0] #拿到ture
            w=s[1]  #拿到1
            alls = Question.objects.filter(Q(id=w) & Q(selectoption=q)).values('score')  #联合查询,并且
            print(alls)
            for j in alls:  #循环查询到的对象
                a+=j['score']
        print(a)
        return HttpResponse('分数为:%s分'%a)


def stu(request):
    return render(request,'stu.html')
